﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using MTechApplicationServices.Contracts;
using MTechApplicationServices.Data;
using System.Net.Mail;
using System.Web.Configuration;

namespace MTechApplicationServices.Activities
{  

    public sealed class NotifyApplicant : CodeActivity
    {        
        public InArgument<AppStatus> Status { get; set; }
        public InArgument<ApplicantInfo> Info { get; set; }
      
        protected override void Execute(CodeActivityContext context)
        {
            AppStatus status = Status.Get(context);
            ApplicantInfo info = Info.Get(context);

            string baseURI = WebConfigurationManager.AppSettings["BaseURI"];

            if (string.IsNullOrEmpty(baseURI))
                throw new InvalidOperationException("No baseURI appSetting found in web.config");

            string htmlMailText;

            if (status == AppStatus.ShortListed)
            {
                htmlMailText = string.Format(ServiceResources.GenericMailTemplate,
                    ServiceResources.SuccessHeading,
                    string.Format(ServiceResources.OfferText, info.Name), baseURI);
            }
            else
            {
                htmlMailText = string.Format(ServiceResources.GenericMailTemplate,
                    ServiceResources.NoHireHeading,
                    string.Format(ServiceResources.RejectText, info.Name), baseURI);
            }

            SmtpClient smtpClient = new SmtpClient();

            MailMessage msg = new MailMessage("admin@contoso.com", info.Email,
                string.Format(ServiceResources.ApplicationMailSubject), htmlMailText);
            msg.IsBodyHtml = true;

            smtpClient.Send(msg);
        }
    }
}
